package cn.scu.ne04jdemo.controller;


import cn.scu.ne04jdemo.entitiy.EarningRight;
import cn.scu.ne04jdemo.entitiy.OwnershipRight;
import cn.scu.ne04jdemo.entitiy.SupervisionRight;
import cn.scu.ne04jdemo.entitiy.UseRight;
import cn.scu.ne04jdemo.service.EarningRightService;
import cn.scu.ne04jdemo.service.OwnershipRightService;
import cn.scu.ne04jdemo.service.SupervisionRightService;
import cn.scu.ne04jdemo.service.UseRightService;
import cn.scu.ne04jdemo.util.ExcelUploadListener;
import com.alibaba.excel.EasyExcel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/rightdata/import")
public class RightDataImportController {

    @Autowired
    private EarningRightService earningRightService;
    @Autowired
    private OwnershipRightService ownershipRightService;
    @Autowired
    private UseRightService useRightService;
    @Autowired
    private SupervisionRightService supervisionRightService;




    @PostMapping("/upload")
    @ResponseBody
    public boolean upload(MultipartFile file, String rightType)  {
        try {
            if(StringUtils.equals("01000", rightType)){//所有权
//                List<OwnershipRight> list = new ArrayList<>();
//                OwnershipRight o = new OwnershipRight();
//                o.setAgentId("1111");
//                list.add(o);
//                ownershipRightService.save(o);
                EasyExcel.read(file.getInputStream(), OwnershipRight.class, new ExcelUploadListener<OwnershipRight>(ownershipRightService)).sheet().headRowNumber(1).doRead();
            }else if(StringUtils.equals("02000", rightType)){//使用权
                EasyExcel.read(file.getInputStream(), UseRight.class, new ExcelUploadListener<UseRight>(useRightService)).sheet().headRowNumber(1).doRead();
            }else if(StringUtils.equals("03000", rightType)){//收益权
                EasyExcel.read(file.getInputStream(), EarningRight.class, new ExcelUploadListener<EarningRight>(earningRightService)).sheet().headRowNumber(1).doRead();
            }else if(StringUtils.equals("04000", rightType)){//监督权
                EasyExcel.read(file.getInputStream(), SupervisionRight.class, new ExcelUploadListener<SupervisionRight>(supervisionRightService)).sheet().headRowNumber(1).doRead();
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

}
